﻿#include <QCoreApplication>
#include"opencv.hpp"
using namespace cv;
using namespace std;

#include<iostream>



Mat getBigmask1(Mat src){

//  equalizeHist(src, src);
//  threshold(src, src, 185, 255, 0);
//  namedWindow("threshold",WINDOW_FREERATIO);
//  imshow("hline",src);
//  Mat hline = getStructuringElement(MORPH_RECT, Size(13, 13), Point(-1, -1));
//  erode(src, src, hline);
//  dilate(src, src, hline);
//  Mat hline11 = getStructuringElement(MORPH_RECT, Size(10, 10), Point(-1, -1));
//  dilate(src, src, hline11);

//    equalizeHist(src, src);
    threshold(src, src, 80, 255, 0);
    namedWindow("ringhtthreshold",WINDOW_FREERATIO);
    imshow("ringhtthreshold",src);
    Mat hline = getStructuringElement(MORPH_RECT, Size(2, 40), Point(-1, -1));
    erode(src, src, hline);
//    dilate(src, src, hline);
//    Mat hline11 = getStructuringElement(MORPH_RECT, Size(10, 10), Point(-1, -1));
//    dilate(src, src, hline11);

  namedWindow("ringt erode",WINDOW_FREERATIO);
  imshow("ringt erode",src);

  Canny(src, src, 50, 200);

  namedWindow("Canny",WINDOW_FREERATIO);
  imshow("Canny",src);

  Mat hline1 = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));
  dilate(src, src, hline1);
//  erode(src, src, hline1);

  namedWindow("rightdilate",WINDOW_FREERATIO);
  imshow("rightdilate",src);

     std::vector<std::vector<cv::Point>> contours;
      std::vector<cv::Vec4i> hierarchy;
      Mat ellipse_mask = Mat::zeros(src.rows,src.cols,CV_8UC1);
      Mat contours_mask = Mat::zeros(src.rows,src.cols,CV_8UC1);
      Mat mask111 = Mat::zeros(src.rows,src.cols,CV_8UC3);
    //只输出最顶层轮廓
      float contours_area;
    findContours(src, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
    cv::RotatedRect ellipse_rect;
    vector<Moments> mo(contours.size());
    Point2f moCenter;
    for (int i = 0; i < contours.size(); i++)
    {

      if(contours[i].size() > 15)
      {
         ellipse_rect = fitEllipse(contours[i]);
         cv::Scalar color;
                 color = cv::Scalar(50,128,200);
         int w = ellipse_rect.size.width;
         int h = ellipse_rect.size.height;
         if(w<src.cols/3||h<src.rows/3)
           continue;
           mo[i] = moments(contours[i], false);

             moCenter = Point2f(mo[i].m10 / mo[i].m00, mo[i].m01 / mo[i].m00);
          ellipse_rect.center.y = ellipse_rect.center.y-5;
          ellipse(ellipse_mask, ellipse_rect, cv::Scalar(255,128,200), 2, 8);
          floodFill(ellipse_mask,ellipse_rect.center,cv::Scalar(255,128,200));
          drawContours(contours_mask, contours, i, cv::Scalar(255,0,128), 1, 8, hierarchy, 0);
          floodFill(contours_mask,ellipse_rect.center,cv::Scalar(255,128,200));
      cout<<"contours.size()"<<contours.size()<<endl;
      }
    }

    Mat hline111 = getStructuringElement(MORPH_RECT, Size(30, 20), Point(-1, -1));
    erode(ellipse_mask, ellipse_mask, hline111);
    Mat result = ellipse_mask-contours_mask;
    namedWindow("rightresultpre",WINDOW_FREERATIO);
    imshow("rightresultpre",result);
    int ww = ellipse_rect.size.width;
    int hh = ellipse_rect.size.height;
   int  w = max(ww,hh);
    int h  = min(ww,hh);
    int avg = (w+h)/2;
    //去掉右侧
    for(int x=moCenter.y;x<result.rows;++x)//img.cols-
         {
           for(int y=moCenter.x+w/4;y<result.cols;++y)//img.rows-
           {
               result.ptr(x)[y] = 0;

             }
           }
    namedWindow("result3",WINDOW_FREERATIO);
    imshow("result3",result);
    for(int x=moCenter.y;x<result.rows;++x)//img.cols-
         {
           for(int y = 0;y<moCenter.x-w/8;++y)//img.rows-
           {
               result.ptr(x)[y] = 0;

             }
           }
    namedWindow("result2",WINDOW_FREERATIO);
    imshow("result2",result);
    Mat hline11 = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
    dilate(result, result, hline11);
    namedWindow("result21",WINDOW_FREERATIO);
    imshow("result21",result);
    return result;
}


Mat you(Mat full)
{
  cv::Mat src = full.clone();

  resize(src,src,Size(src.cols*3,src.rows*3));
  namedWindow("src",WINDOW_FREERATIO);
  imshow("src",src);
  if(src.channels()!=1)
  cvtColor(src, src, COLOR_BGR2GRAY);
  Mat ori = src.clone();

  //得到大圆
  Mat mask =   getBigmask1(src);
  namedWindow("mask",WINDOW_FREERATIO);
  imshow("mask",mask);

  std::vector<std::vector<cv::Point>> contours;
   std::vector<cv::Vec4i> hierarchy;
   Mat ellipse_mask = Mat::zeros(src.rows,src.cols,CV_8UC1);
   Mat contours_mask = Mat::zeros(src.rows,src.cols,CV_8UC1);
   Mat mask111 = Mat::zeros(src.rows,src.cols,CV_8UC3);
 //只输出最顶层轮廓
   float contours_area;
 findContours(mask, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
 cv::RotatedRect ellipse_rect;
 for (int i = 0; i < contours.size(); i++)
 {
   if(contours[i].size() > 7)
   {
      ellipse_rect = fitEllipse(contours[i]);
       drawContours(contours_mask, contours, i, cv::Scalar(255,0,128), 1, 8, hierarchy, 0);
//       floodFill(contours_mask,ellipse_rect.center,cv::Scalar(255,128,200));
   }
 }
 namedWindow("contours_mask",WINDOW_FREERATIO);
 imshow("contours_mask",contours_mask);
 Mat hline11 = getStructuringElement(MORPH_RECT, Size(2, 1), Point(-1, -1));
 erode(contours_mask, contours_mask, hline11);
 return contours_mask;


 Mat hline2 = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
 dilate(contours_mask, contours_mask, hline2);
 Mat hline = getStructuringElement(MORPH_RECT, Size(5, 1), Point(-1, -1));
 erode(contours_mask, contours_mask, hline);
 dilate(contours_mask, contours_mask, hline);
 namedWindow("contours_mask1",WINDOW_FREERATIO);
 imshow("contours_mask1",contours_mask);
    Mat big_src = ori & mask;
}


